from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from l2ldjango.apps.usermanagement.models import *
from l2ldjango.apps.personalblog.models import *
from l2ldjango.apps.blog.models import *
from l2ldjango.apps.category.models import * 
from django.contrib.localflavor.us.models import USStateField
from django.conf import settings

USER_TYPES = ((1,'ATTORNEY'),
                      (2, 'STUDENT')
                      )

FRIEND_STATUS=(
               ('PENDING','PENDING'),
               ('APPROVED','APPROVED'),
               ('CANCELED','CANCELED'),
               ('NOTNOW','NOTNOW')                
               )

MH_RATINGS = (('AV Preeminent','AV Preeminent'),
              ('BV Distinguished','BV Distinguished'),
              ('Rated','Rated')
              )


YEAR_CHOICES = [(1950+x,'%d' % (1950+x)) for x in range(2013-1950)]
                      
MEMBERSHIP_TYPES=(('NORMAL','NORMAL'),('PREMIUM','PREMIUM'))

class County(models.Model):
    county = models.CharField(max_length=500)
    def __unicode__(self):
        return self.county
    class Meta:
        ordering = ('county',)

class PracticeArea(models.Model):
    title=models.CharField(max_length=255,null=False,blank=False,verbose_name="Practice Area")
    def __str__(self):
        return self.title 
    class Meta:
        ordering = ('title',)

class Phone(models.Model):
    office=models.CharField(max_length=30,null=True,blank=True,verbose_name="Office Telephone")
    home=models.CharField(max_length=30,null=True,blank=True,verbose_name="Home Telephone")
    fax=models.CharField(max_length=30,null=True,blank=True,verbose_name="Fax")
    mobile=models.CharField(max_length=30,null=True,blank=True,verbose_name="Mobile")
    
class status(models.Model):
    status=models.CharField(max_length=255,null=False,verbose_name="Status*")
    code=models.CharField(max_length=50,unique=True,null=False,blank=False,verbose_name="Status Code*")
    def __str__(self):
        return self.status

class ExternalLink(models.Model):
    title=models.CharField(max_length=255,null=False,blank=False,verbose_name="Link Title")
    link=models.URLField(null=False,blank=False)
    user=models.ForeignKey(User,null=True,blank=True,related_name='external_links')

class ImageGallery(models.Model):
    Title=models.CharField(max_length=220,null=True,blank=True,verbose_name="Image Title")
    Description=models.TextField(null=True,blank=True,verbose_name="Image Description")
    image=models.ImageField(null=False,blank=False,upload_to='profileimages')
    imgname = models.CharField(max_length=250,null=True, blank=True)
    profile_image=models.BooleanField(default=False)
    user=models.ForeignKey(User,null=True,blank=False)	
    

class Department(models.Model):
    department = models.CharField(max_length=500)
    def __unicode__(self):
        return self.department
    class Meta:
        ordering = ('department',)
    
class userprofile(models.Model):
    #phone=models.CharField(max_length=255,null=True,blank=True,verbose_name="Phone")
    #address=models.CharField(max_length=255,null=False,blank=False,verbose_name="Address")
    #city=models.CharField(max_length=255,null=False,blank=False,verbose_name="City")
    user_type=models.IntegerField(null=True,blank=True,choices=USER_TYPES)
    profile_image = models.ImageField( verbose_name='Profile Image', upload_to='profileimages',
                              blank=True,null=True,help_text='Profile Image')
    licence=models.CharField(blank=True,null=True,max_length=225,verbose_name="Licence Number (for Attorney)")
    verified=models.BooleanField(default=False)
    visible_in_search=models.BooleanField(verbose_name="Visible In Public Search")
    target_graduation_date=models.DateField(blank=True,null=True,verbose_name="Target Graduation Year(for Students)")
    
    user = models.OneToOneField(User,null=True,blank=True)
    
    fb_id=models.CharField(max_length=225,null=True,blank=True)
    
    full_name = models.CharField(max_length=400,null=True,blank=True)
    
    first_name = models.CharField(max_length=400,null=True,blank=True)
    middle_name = models.CharField(max_length=400,null=True,blank=True)
    last_name = models.CharField(max_length=400,null=True,blank=True)
    
    #added_later after client's email
    #current_fname=models.CharField(max_length=20,null=False,blank=False,verbose_name="Current First Name")
    current_mname=models.CharField(max_length=20,null=True,blank=True,verbose_name="Middle Initial")
    #current_lname=models.CharField(max_length=30,null=False,blank=False,verbose_name="Current Last Name")
    #prior_fname=models.CharField(max_length=20,null=True,blank=True,verbose_name="Prior Middle Name")
    #prior_mname=models.CharField(max_length=20,null=True,blank=True,verbose_name="Prior Middle Name")
    #prior_lname=models.CharField(max_length=30,null=True,blank=True,verbose_name="Prior Last Name")
    #company_name=models.CharField(max_length=100,null=True,blank=True,verbose_name="Prior Last Name")
    address=models.CharField(max_length=150,null=True,blank=True,verbose_name="Address")
    room=models.CharField(max_length=20,null=True,blank=True,verbose_name="Room")
    floor=models.CharField(max_length=20,null=True,blank=True,verbose_name="Floor")
    address2=models.CharField(max_length=100,null=True,blank=True,verbose_name="Address2")
    address3=models.CharField(max_length=100,null=True,blank=True,verbose_name="Address3")
    city=models.CharField(max_length=30,null=True,blank=True,verbose_name="City")
    state=USStateField(null=True,blank=True,verbose_name="State")
    
    email = models.EmailField(null=True,blank=True)
    
    zip=models.IntegerField(null=True,blank=True,verbose_name="Zip")
    #zip4=models.IntegerField(null=True,blank=True,verbose_name="Zip4")
    telephone= models.CharField(max_length=20,null=True,blank=True,verbose_name="Mobile")
    office=models.CharField(max_length=30,null=True,blank=True,verbose_name="Office Telephone")
    home=models.CharField(max_length=30,null=True,blank=True,verbose_name="Home Telephone")
    fax=models.CharField(max_length=30,null=True,blank=True,verbose_name="Fax")
    #mobile=models.CharField(max_length=30,null=True,blank=True,verbose_name="Mobile")
    admitted_year=models.IntegerField(choices=YEAR_CHOICES,null=True,blank=True,verbose_name="Admitted Year")
    
    office_county = models.ForeignKey(County,null=True,blank=True)
    department = models.ForeignKey(Department,null=True,blank=True)
    
    dob=models.DateField(blank=True,null=True,verbose_name="DOB")
    last_contact_date=models.DateField(blank=True,null=True,verbose_name="Last Contact Date")
    user_status=models.ForeignKey(status,null=True,blank=True)
    last_contact_reg_period_start_year=models.CharField(max_length=100,null=True,blank=True,verbose_name="Last Contact Reg period Start Year")
    last_contact_reg_period = models.CharField(max_length=300,null=True,blank=True)
    #filler=models.CharField(max_length=48,null=True,blank=True,verbose_name="Filler")
    old_imported=models.BooleanField(default=False)
    verification_code=models.CharField(null=True,blank=True,max_length=10)
    #primary_area=models.CharField(null=True,blank=True,max_length=225)
    #secondary_areas=models.TextField(null=True,blank=True)
    primary_area=models.ForeignKey(PracticeArea,null=True,blank=True,related_name="primary_area")
    secondary_areas=models.ForeignKey(PracticeArea,null=True,blank=True,related_name="secondary_areas")
    
    bio=models.TextField(null=True,blank=True)
    first_time=models.BooleanField(default=True)
    membership=models.CharField(blank=True,max_length=20,null=True,choices=MEMBERSHIP_TYPES)
    fb_link = models.CharField(max_length=20,null=True,blank=True, verbose_name="Facebook Link")
    web_link = models.CharField(null=True,blank=True,max_length=100, verbose_name="")
    blg_link = models.CharField(null=True,blank=True,max_length=100, verbose_name="")
    lnkd_link = models.CharField(null=True,blank=True,max_length=100, verbose_name="")
    fvrt_blg_link = models.CharField(null=True,blank=True,max_length=100, verbose_name="")
    mh_rating = models.CharField(choices=MH_RATINGS,null=True,blank=True,max_length=50, verbose_name="")
    enjoying = models.TextField(null=True,blank=True, verbose_name="")
    
    accepting_referrals = models.BooleanField()
    available_per_diem = models.BooleanField()
    
    def __unicode__(self):
        return_string = []
        if self.first_name:
            return_string.append(self.first_name)
        if self.middle_name:
            return_string.append(self.middle_name)
        if self.last_name:
            return_string.append(self.last_name)
        return " ".join(return_string)
    
    @property
    def secondary_areas_string(self):
        return ", ".join([x.title for x in self.secondary_areas.all()])
    
    class Meta:
        ordering = ('last_name',)
        permissions = (
            ("post_jobs", "Can Post jobs for students"),
            ("create_page", "Can create custom page"),
            ("contribute_case_info", "Can Contribute case information"),
            ("content_control", "Can add/edit all contents"),
            ("selt_content_control", "Can add/edit own contents only"),
            ("send_referrals", "Can send refererrals to friends"),
            ("forum_access", "Can use L2L Forum"),
        )
        
    def save(self,*args,**kwargs):
        self.full_name = '%s %s' % (self.first_name,self.last_name)
        super(userprofile,self).save(*args,**kwargs)
        
        
class Friends(models.Model):
    initiator=models.ForeignKey(User,null=False,blank=False,related_name='initiator')
    #receiver=models.IntegerField(null=False)
    receiver=models.ForeignKey(User,null=False,blank=False,related_name='receiver')
    verification_code=models.CharField(max_length=255,null=False,blank=False)
    date_request_sent=models.DateTimeField(verbose_name='Request Sent Date', auto_now_add=True)
    status=models.CharField(max_length=20,choices=FRIEND_STATUS, default='PENDING')
    is_favourite=models.BooleanField(default=False)
    
#class Referrals(models.Model):
#    referee=models.ForeignKey(User,null=False,blank=False,related_name='referee')
#    refered_to=models.ForeignKey(User,null=False,blank=False,related_name='refered_to')
#    refered_by=models.ForeignKey(User,null=False,blank=False,related_name='refered_by')
#    refered_date=models.DateTimeField(verbose_name='Referred Date', auto_now_add=True)
#    def __str__(self):
#        return self.referee

        
class Bookmarks(models.Model):
    blog_personal=models.ManyToManyField(PersonalBlog,null=True,blank=True,verbose_name='Personal Blogs',related_name="blog_personal")
    user=models.ForeignKey(User,null=False,blank=False)
    categories=models.ManyToManyField(Category,verbose_name='Categories',null=True,blank=True)
    blog_site=models.ManyToManyField(Blog,null=True,blank=True,verbose_name='Site Blogs',related_name="blog_site")
    
class Memberships(models.Model):
    user=models.ForeignKey(User,null=True)
    subscription_date = models.DateField(auto_now_add=True)
    